{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MFEM Example 1\n",
    "\n",
    "Adapted from [PyMFEM/ex1.py]( https://github.com/mfem/PyMFEM/blob/master/examples/ex1.py).\n",
    "Compare with the [original Example 1](https://github.com/mfem/mfem/blob/master/examples/ex1.cpp) in MFEM.\n",
    "\n",
    "This example code demonstrates the use of MFEM to define a simple finite element discretization of the Laplace problem\n",
    "\n",
    "\\begin{equation*}\n",
    "-\\Delta x = 1\n",
    "\\label{laplace}\\tag{1}\n",
    "\\end{equation*}\n",
    "\n",
    "in a domain $\\Omega$ with homogeneous Dirichlet boundary conditions\n",
    "\n",
    "\\begin{equation*}\n",
    "x = 0\n",
    "\\label{laplace2}\\tag{2}\n",
    "\\end{equation*}\n",
    "\n",
    "on the boundary $\\partial \\Omega$.\n",
    "\n",
    "The problme is discretized on a computational mesh in either 2D or 3D using a finite elements space of the specified order (2 by default) resulting in the global sparse linear system\n",
    "\n",
    "\\begin{equation*}\n",
    "A X = B.\n",
    "\\label{laplace3}\\tag{3}\n",
    "\\end{equation*}\n",
    "\n",
    "The example highlights the use of mesh refinement, finite element grid functions, as well as linear and bilinear forms corresponding to the left-hand side and right-hand side of the\n",
    "discrete linear system. We also cover the explicit elimination of essential boundary conditions and using the GLVis tool for visualization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Requires pymfem and pyglvis see https://github.com/glvis/pyglvis\n",
    "import mfem.ser as mfem\n",
    "from glvis import glvis, to_stream"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the mesh from a local file\n",
    "# meshfile = '../../mfem/data/star.mesh'\n",
    "# mesh = mfem.Mesh(meshfile)\n",
    "\n",
    "# Alternatively, create a simple square mesh and refine it\n",
    "mesh = mfem.Mesh(5, 5, \"TRIANGLE\")\n",
    "mesh.UniformRefinement()\n",
    "\n",
    "# Create H1 finite element function space\n",
    "fec = mfem.H1_FECollection(2, mesh.Dimension()) # order=2\n",
    "fespace = mfem.FiniteElementSpace(mesh, fec)      \n",
    "\n",
    "# Determine essential degrees of freedom (the whole boundary here)\n",
    "ess_tdof_list = mfem.intArray()\n",
    "ess_bdr = mfem.intArray([1]*mesh.bdr_attributes.Size())\n",
    "fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list)\n",
    "\n",
    "# Define Bilinear and Linear forms for the Laplace problem -Δu=1\n",
    "one = mfem.ConstantCoefficient(1.0)\n",
    "a = mfem.BilinearForm(fespace)\n",
    "a.AddDomainIntegrator(mfem.DiffusionIntegrator(one))\n",
    "a.Assemble()\n",
    "b = mfem.LinearForm(fespace)\n",
    "b.AddDomainIntegrator(mfem.DomainLFIntegrator(one))\n",
    "b.Assemble()\n",
    "\n",
    "# Create a grid function for the solution and initialize with 0\n",
    "x = mfem.GridFunction(fespace);\n",
    "x.Assign(0.0)\n",
    "\n",
    "# Form the linear system, AX=B, for the FEM discretization\n",
    "A = mfem.OperatorPtr()\n",
    "B = mfem.Vector()\n",
    "X = mfem.Vector()\n",
    "a.FormLinearSystem(ess_tdof_list, x, b, A, X, B);\n",
    "print(\"Size of the linear system: \" + str(A.Height()))\n",
    "\n",
    "# Solve the system using PCG solver and get the solution in x\n",
    "Asm = mfem.OperatorHandle2SparseMatrix(A)\n",
    "Msm = mfem.GSSmoother(Asm)\n",
    "mfem.PCG(Asm, Msm, B, X, 1, 200, 1e-12, 0.0)\n",
    "a.RecoverFEMSolution(X, b, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot the Solution with GLVis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the mesh + solution (all GLVis keys and mouse commands work)\n",
    "glvis((mesh, x), 400, 400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the mesh only\n",
    "glvis(mesh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Visualization with additional GLVis keys\n",
    "g = glvis(to_stream(mesh,x) + 'keys ARjlmcbp*******')\n",
    "g.set_size(600, 400)\n",
    "g"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
